import Vue from "vue"
import axios from 'axios'
import router from '../router'
// import router from 'vue-router'

Vue.prototype.$ajax=axios;
Vue.prototype.baseUrl="http://localhost:8081/";
axios.defaults.baseURL = "http://localhost:8081/";

//请示拦截器
axios.interceptors.request.use(function(config){
	var token=sessionStorage.getItem("token");
	if(token){
		config.headers.Authorization=token;
	}
	if(config.method !='get'){
		var sendData = new FormData();
		var data = config.data;
		Object.keys(data).forEach(key => {
			sendData.append(key, data[key]);
		});
		config.data = sendData;
	}
	return config
},function(error){
	return Promise.reject(error)
});


//响应拦截器
axios.interceptors.response.use(res=>res,error=>{
	if(error.response){
		// console.log(error.response.status);
		switch(error.response.status){
			case 401:
				window.sessionStorage.removeItem("name");
				window.sessionStorage.removeItem("token");
				router.replace({
					path: "/",
				});
		}
	}
	return Promise.reject(error);
})
export default axios

